{extend name="common/index"}
{block name="css"}
<style>
    .layui-table-cell{
        padding: 0 8px;
    }
</style>
{/block}
{block name="content"}
<!--条件搜索栏-->
<div class="layui-row">
    <form id="searchForm" class="layui-form layui-form-pane tpl-form-search" onkeydown="if(event.keyCode=='13'){reloadTable('#searchForm');return false;}">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">位置</label>
                <div class="layui-input-inline">
                    <select name="location">
                        <option value="">请选择</option>
                        {volist name="locationList" id="vo"}
                        <option value="{$vo.basic_id}">{$vo.basic_name}</option>
                        {/volist}
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="img_name" placeholder="名称" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-btn-group">
                    <a class="layui-btn layui-btn-normal" onclick="reloadTable('#searchForm');">搜索</a>
                    <a class="layui-btn"  href="{:url('edit')}">新增</a>
                </div>
            </div>
        </div>
    </form>
</div>
<!--数据列表-->
<div class="layui-btn-group">
    <button class="layui-btn layui-btn-primary layui-btn-sm" onclick="ajaxDelete(getSelectedIdList(), '{:url("delete")}');">
        <i class="layui-icon layui-icon-delete"></i>删除选中
    </button>
</div>
<table id="bannerTable" lay-filter="bannerTable"></table>
{/block}

{block name="script"}
<script type="text/javascript">
    var table;
    layui.use(['form','laydate', 'table', 'layer'], function () {
        var form = layui.form;
        var laydate = layui.laydate;
        table = layui.table;//全局变量，方便调用删除方法
        var layer = layui.layer;

        /*注册日期，执行一个laydate实例*/
        laydate.render({elem: '#public_begin'});
        laydate.render({elem: '#public_end'});

        //表格数据渲染
        tableIns = table.render({
            elem: '#bannerTable'
            ,url: '{:url("getDataList")}'
            ,page: true
            ,limit: table_limit
            ,limits: table_limits
            ,cols: [[
                {type:'checkbox', width: 45}
                ,{field:'location_text', title:'banner位置', width: '10%', align:'center'}
                ,{title:'图片' , width: '10%', align:'center', templet: function (data) {
                    return data.img_url != '' ? '<a href="'+data.img_url+'" target="_blank"><img src="'+data.img_url+'" alt="" style="height: 35px;"></a>' : "";
                }}
                ,{field:'img_name', title:'名称', width: '18%'}
                ,{field:'state_text', title:'是否显示', align:"center", width: 100}
                ,{field:'link', title:'链接', templet: function (data) {
                    return data.link != '' ? '<a href="'+data.link+'" target="_blank">'+data.link+'</a>' : "";
                }}
                ,{title:'操作', width: 135, align:'center', templet: function (data) {
                    var _html = '<div class="layui-btn-group">';
                    _html += '<a class="layui-btn layui-btn-xs" href="javascript:;" lay-event="edit">编辑</a>';
                    _html += '<a class="layui-btn layui-btn-danger layui-btn-xs" href="javascript:;" lay-event="del">删除</a>';
                    _html += '</div>';
                    return _html;
                }}
            ]]
            ,where: getFormJson('#searchForm')
        });

        //监听工具条
        table.on('tool(bannerTable)', function(obj){
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('数据删除后无法恢复，确定继续吗？', function(index){
                    ajaxDelete(data.banner_id, '{:url("delete")}');
                });
            } else if (obj.event === 'edit') {
                window.location.href = 'edit?banner_id='+data.banner_id;
            }
        });

    });

    //获取当前选中的id，数组
    function getSelectedIdList() {
        var checkList = table.checkStatus('bannerTable');
        var id_list = [];
        if (checkList.data.length>0) {
            $.each(checkList.data, function (key, val) {
                id_list[key] = val.banner_id;
            });
        }
        return id_list;
    }

</script>
{/block}